package com.db.server.task;

import java.util.LinkedList;
import java.util.List;

/**
 * Blocking queue for sending and receiving messages.
 * Linked list is used inside for fast insert in the end and fast remove from the front.
 */
public class TaskQueue {
    private List<Task> queue = new LinkedList<Task>();

    public void add(Task task) {
        synchronized (queue) {
            queue.add(task);
            queue.notifyAll();
        }
    }

    public Task remove() {
        synchronized (queue) {
            try {
                while (queue.isEmpty()) {
                    queue.wait();
                }
                return queue.remove(0);
            } catch (InterruptedException e) {
                return null;
            }
        }
    }

}
